package com.banciyuan.bcywebview.biz.groupdetail;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by squall on 2015/8/25.
 */
public class AutoComplateAdapter extends BaseAdapter implements Filterable {

    private List<String> slist=new ArrayList<String>();

    private Context mContext;

    private LayoutInflater inflater;

    private ArrayFilter arrayFilter;

    private String orign;


    public AutoComplateAdapter(List<String> slist,Context mContext,String orign){
        this.slist=slist;
        this.mContext=mContext;
        this.inflater=LayoutInflater.from(mContext);
        this.orign=orign;
        arrayFilter=new ArrayFilter();
    }

    @Override
    public int getCount() {
        if(slist.size()==0)return 1;
        else return slist.size();
    }

    @Override
    public Object getItem(int i) {
        if(slist.size()==0)return "";
        else return slist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Filter getFilter() {
        if(arrayFilter==null)
            return new ArrayFilter();
        else
            return arrayFilter;
    }

    private class ViewHodler{
        TextView title;
        TextView front;
        TextView back;
        LinearLayout background;

        public ViewHodler(View view){
            title= (TextView) view.findViewById(R.id.auto_complate);
            front= (TextView) view.findViewById(R.id.auto_front);
            back= (TextView) view.findViewById(R.id.auto_back);
            background= (LinearLayout) view.findViewById(R.id.auto_background);
        }
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHodler viewHodler=null;
        if(view==null){
            view=inflater.inflate(R.layout.autocomplate_item,null);
            viewHodler=new ViewHodler(view);
            view.setTag(viewHodler);
        }else{
            viewHodler= (ViewHodler) view.getTag();
        }

        if(slist.size()==0){
            viewHodler.front.setVisibility(View.VISIBLE);
            viewHodler.back.setVisibility(View.VISIBLE);
            viewHodler.title.setText("\"" + orign + "\"");
            viewHodler.front.setText(mContext.getString(R.string.not_found));
            viewHodler.back.setText(mContext.getString(R.string.relation_content));
            viewHodler.title.setTextColor(mContext.getResources().getColorStateList(R.color.pink));
        }else{
            viewHodler.front.setVisibility(View.GONE);
            viewHodler.back.setVisibility(View.GONE);
            viewHodler.title.setText(slist.get(i));
            viewHodler.title.setTextColor(mContext.getResources().getColorStateList(R.color.mine_textcolor));
        }

        return view;
    }

    private class ArrayFilter extends Filter{

        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {

            FilterResults results=new FilterResults();

            results.values=slist;
            results.count=slist.size();

            return results;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            slist = (List<String>) filterResults.values;
            notifyDataSetChanged();
        }
    }
}
